package zdh.avatar.gui;

import java.awt.Dimension;
import java.awt.event.*;
import java.util.*;
import javax.sound.sampled.Clip;
import javax.swing.*;
import zdh.avatar.base.*;
/**
 *
 * @author Gem Seeker
 */
public class ChildGroup extends JPanel implements ActionListener {

    private MainFrame main;
    private ButtonGroups buttonGroups;
    private Clip currentClip;
    
    public ChildGroup(MainFrame main, ButtonGroups buttonGroups) {
        this.main = main;
        this.buttonGroups = buttonGroups;
        initComponents();
        initListeners();
    }
    
    private void initListeners(){
        btn_stop.addActionListener(this);
        btn_livetransfer.addActionListener(this);
    }
    
    @Override public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if(source == btn_stop){
            if(buttonGroups.getCurrentClip()!=null){
                buttonGroups.getCurrentClip().stop();
            }
        }else if(source == btn_livetransfer){
            if(buttonGroups.getCurrentClip()!=null){
                buttonGroups.getCurrentClip().stop();
            }
            
            // add default clip to play when live transfer is clicked
        }
    }
    
    public void loadButtons(ElementButton button){
        panel.removeAll();
        tabbedPane.removeAll();
        
        ArrayList<ElementButton> btns = button.getChildButtons();
        System.out.println(btns);
        for(ElementButton b: btns){
            JButton btn = createButton(b);
            btn.addActionListener(new ButtonHandler(b, this));
            panel.add(btn);
        }
        
        tabbedPane.addTab("Recommended Buttons...", panel);
    }
    
    private JButton createButton(ElementButton button){
        JButton b = new JButton(button.getButtonName());
        b.setBackground(button.getButtonColor());
        b.setToolTipText(button.getButtonName());
        b.setOpaque(true);
        b.setPreferredSize(new Dimension(150, 30));
        return b;
    }
    
    public void setCurrentClip(Clip clip){
        this.currentClip = clip;
    }
    
    public Clip getCurrentClip(){
        return currentClip;
    }

    //<editor-fold defaultstate="collapsed" desc="Generated by NetBeans">
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        panel = new javax.swing.JPanel();
        btn_stop = new javax.swing.JButton();
        btn_livetransfer = new javax.swing.JButton();

        setOpaque(false);

        panel.setBackground(new java.awt.Color(255, 255, 255));
        panel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        tabbedPane.addTab("Recommended Buttons...", panel);

        btn_stop.setText("Stop");

        btn_livetransfer.setText("Live Transfer");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_stop, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_livetransfer)
                        .addGap(0, 379, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_stop)
                    .addComponent(btn_livetransfer))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_livetransfer;
    private javax.swing.JButton btn_stop;
    private javax.swing.JPanel panel;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
    //</editor-fold>
}
